﻿/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "mouseui.h"

MouseUI::MouseUI() : mFirstLoad(true)
{
    pluginName = tr("Mouse");
    pluginType = DEVICES;
}

MouseUI::~MouseUI()
{
}

QString MouseUI::plugini18nName()
{
    return pluginName;
}

int MouseUI::pluginTypes()
{
    return pluginType;
}

QWidget *MouseUI::pluginUi()
{
    if (mFirstLoad) {
        mFirstLoad = false;
        pluginWidget = new QWidget;
        pluginWidget->setAttribute(Qt::WA_DeleteOnClose);

        initUI(pluginWidget);
        initConnection();
        if (Common::isTablet()) {
            settingForIntel();
        }
    }
    return pluginWidget;
}

const QString MouseUI::name() const
{
    return QStringLiteral("Mouse");
}

bool MouseUI::isShowOnHomePage() const
{
    return true;
}

QIcon MouseUI::icon() const
{
    return QIcon::fromTheme("input-mouse-symbolic");
}

bool MouseUI::isEnable() const
{
    return true;
}

void MouseUI::settingForIntel()
{
    if (pluginWidget) {
        mWheelSpeedWidget->hide();
        mDoubleClickIntervalWidget->hide();
        mScrollDirectionWdiget->setFrameShape(QFrame::HLine);
        mMouseAccelerationWidget->hide();
    }
}

MyLabel::MyLabel()
{
    setAttribute(Qt::WA_DeleteOnClose);

    QSizePolicy pSizePolicy = this->sizePolicy();
    pSizePolicy.setHorizontalPolicy(QSizePolicy::Fixed);
    pSizePolicy.setVerticalPolicy(QSizePolicy::Fixed);
    this->setSizePolicy(pSizePolicy);

    setFixedSize(QSize(54, 28));
    setScaledContents(true);

    setPixmap(QPixmap(":/img/plugins/mouse/double-click-off.png"));

    const QByteArray id(kMouseSchemas);
    if (QGSettings::isSchemaInstalled(id)){
        mSettings = new QGSettings(id, QByteArray(), this);
    }
    this->setToolTip(tr("double-click to test"));
}

MyLabel::~MyLabel()
{
}

void MyLabel::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    int delay = mSettings->get(kDoubleClickIntervalKey).toInt();
    setPixmap(QPixmap(":/img/plugins/mouse/double-click-on.png"));
    QTimer::singleShot(delay, this, [=]{
        setPixmap(QPixmap(":/img/plugins/mouse/double-click-off.png"));
    });
}

void MouseUI::initUI(QWidget *widget)
{
    mVlayout = new QVBoxLayout(widget);
    mVlayout->setContentsMargins(0, 0, 0, 0);

    //鼠标
    SettingGroup *mouseFrame = new SettingGroup(this);

    //指针
    SettingGroup *pointerFrame = new SettingGroup(this);

    //光标
    SettingGroup *cursorFrame = new SettingGroup(this);

    mMouseTitleLabel = new TitleLabel(this);
    mMouseTitleLabel->setText(tr("Mouse"));
    mMouseTitleLabel->setContentsMargins(16,0,0,0);
    setDominantHandFrame();
    setScrollDirectionFrame();
    setWheelSpeedFrame();
    setDoubleClickFrame();

    mPointerTitleLabel = new TitleLabel(this);
    mPointerTitleLabel->setText(tr("Pointer"));
    mPointerTitleLabel->setContentsMargins(16,0,0,0);
    setPointerSpeedFrame();
    setMouseAccelerationFrame();
    setPointerPositionFrame();
    setPointerSizeFrame();

    mCursorTitleLabel = new TitleLabel(this);
    mCursorTitleLabel->setText(tr("Cursor"));
    mCursorTitleLabel->setContentsMargins(16,0,0,0);
    setBlinkCursorFrame();
    setCursorSpeedFrame();

    /* add all frame to widget */
    mouseFrame->insertWidget(0, mDominantHandWidget);
    mouseFrame->insertWidget(1, mScrollDirectionWdiget);
    mouseFrame->insertWidget(2, mWheelSpeedWidget);
    mouseFrame->insertWidget(3, mDoubleClickIntervalWidget);

    pointerFrame->insertWidget(0, mPointerSpeedWidget);
    pointerFrame->insertWidget(1, mMouseAccelerationWidget);
    pointerFrame->insertWidget(2, mPointerPositionWidget);
    pointerFrame->insertWidget(3, mPointerSizeWidet);

    cursorFrame->insertWidget(0, mBlinkingCursorOnTextWidget);
    cursorFrame->insertWidget(1, mCursorSpeedWidget);

    mVlayout->addWidget(mMouseTitleLabel);
    mVlayout->addWidget(mouseFrame);
    mVlayout->addSpacing(32);
    mVlayout->setSpacing(8);

    mVlayout->addWidget(mPointerTitleLabel);
    mVlayout->addWidget(pointerFrame);
    mVlayout->addSpacing(32);
    mVlayout->setSpacing(8);

    mVlayout->addWidget(mCursorTitleLabel);
    mVlayout->addWidget(cursorFrame);
    mVlayout->addStretch();
}

void MouseUI::setDominantHandFrame()
{
    /* dominant hand Frame Setting */

    //~ contents_path /Mouse/Dominant hand
    mDominantHandWidget = new RadioButtonWidget(tr("Dominant hand"), this, UkccFrame::Top);
    mDominantHandWidget->setObjectName("Dominant hand");
    mDominantHandLeftRadio = new QRadioButton(tr("Left hand"), this);
    mDominantHandRightRadio = new QRadioButton(tr("Right hand"), this);
    mDominantHandWidget->addButton(mDominantHandLeftRadio, 0);
    mDominantHandWidget->addButton(mDominantHandRightRadio, 1, true);

}

void MouseUI::setScrollDirectionFrame()
{
    /* dominant hand Frame Setting */

    //~ contents_path /Mouse/Scroll direction
    mScrollDirectionWdiget = new RadioButtonWidget(tr("Scroll direction"), this);
    mScrollDirectionWdiget->setObjectName("Scroll direction");
    mScrollDirectionForwardRadio = new QRadioButton(tr("Forward"), this);
    mScrollDirectionReverseRadio = new QRadioButton(tr("Reverse"), this);
    mScrollDirectionWdiget->addButton(mScrollDirectionForwardRadio, 0);
    mScrollDirectionWdiget->addButton(mScrollDirectionReverseRadio, 1, true);
}

void MouseUI::setWheelSpeedFrame()
{
    /* wheel speed Frame Setting */

    //~ contents_path /Mouse/Wheel speed
    mWheelSpeedWidget = new SliderWidget(tr("Wheel speed"), true, this);
    mWheelSpeedWidget->setObjectName("Wheel speed");
    mWheelSpeedWidget->setLeftText(tr("Slow"));
    mWheelSpeedWidget->setRightText(tr("Fast"));
    mWheelSpeedWidget->slider()->setSliderType(KSliderType::NodeSlider);
    mWheelSpeedWidget->slider()->setRange(1,10);
    mWheelSpeedWidget->slider()->setTickInterval(1);
    mWheelSpeedWidget->slider()->setPageStep(1);
    mWheelSpeedWidget->slider()->installEventFilter(this);
}

void MouseUI::setDoubleClickFrame()
{
    /* Double Click Interval time Frame Setting */

    //~ contents_path /Mouse/Double-click interval time
    mDoubleClickIntervalWidget = new SliderWidget(tr("Double-click interval time"), false, this, UkccFrame::Bottom);
    mDoubleClickIntervalWidget->setObjectName("Double-click interval time");
    mDoubleClickIntervalWidget->setLeftText(tr("Slow"));
    mDoubleClickIntervalWidget->setRightText(tr("Fast"));
    mDoubleClickIntervalWidget->slider()->setSliderType(KSliderType::SmoothSlider);
    mDoubleClickIntervalWidget->slider()->setMinimum(170);
    mDoubleClickIntervalWidget->slider()->setMaximum(1000);
    mDoubleClickIntervalWidget->slider()->setSingleStep(100);
    mDoubleClickIntervalWidget->slider()->setPageStep(100);
    mDoubleClickIntervalWidget->slider()->installEventFilter(this);
    mDoubleClickIntervalWidget->insertWidget(5, new MyLabel());
}

void MouseUI::setPointerSpeedFrame()
{
    /* Pointer Speed Frame Setting */

    //~ contents_path /Mouse/Pointer speed
    mPointerSpeedWidget = new SliderWidget(tr("Pointer speed"), false, this, UkccFrame::Top);
    mPointerSpeedWidget->setObjectName("Pointer speed");
    mPointerSpeedWidget->setLeftText(tr("Slow"));
    mPointerSpeedWidget->setRightText(tr("Fast"));
    mPointerSpeedWidget->slider()->setSliderType(KSliderType::SmoothSlider);
    mPointerSpeedWidget->slider()->setMinimum(100);
    mPointerSpeedWidget->slider()->setMaximum(1000);
    mPointerSpeedWidget->slider()->setSingleStep(50);
    mPointerSpeedWidget->slider()->setPageStep(50);
    mPointerSpeedWidget->slider()->installEventFilter(this);
}

void MouseUI::setMouseAccelerationFrame()
{
    /* Mouse Acceleration Frame Setting */
    //~ contents_path /Mouse/Mouse acceleration
    mMouseAccelerationWidget = new SwitchWidget(tr("Mouse acceleration"), this, UkccFrame::None);
    mMouseAccelerationWidget->setObjectName("Mouse acceleration");
}

void MouseUI::setPointerPositionFrame()
{
    /* Pointer Position Frame Setting */
    //~ contents_path /Mouse/Show pointer position when pressing ctrl
    mPointerPositionWidget = new SwitchWidget(tr("Show pointer position when pressing ctrl"), this, UkccFrame::None);
    mPointerPositionWidget->setObjectName("Show pointer position when pressing ctrl");
}

void MouseUI::setPointerSizeFrame()
{
    /* Pointer Size Frame Setting */

    //~ contents_path /Mouse/Pointer size
    mPointerSizeWidet = new RadioButtonWidget(tr("Pointer size"), this, UkccFrame::Bottom);
    mPointerSizeWidet->setObjectName("Pointer size");
    mPointerSizeSmallRadio = new QRadioButton(tr("Small(recommend)"), this);
    mPointerSizeMediumRadio = new QRadioButton(tr("Medium"), this);
    mPointerSizeLargeRadio = new QRadioButton(tr("Large"), this);
    mPointerSizeWidet->addButton(mPointerSizeSmallRadio, 24);
    mPointerSizeWidet->addButton(mPointerSizeMediumRadio, 36);
    mPointerSizeWidet->addButton(mPointerSizeLargeRadio, 48, true);
}

void MouseUI::setBlinkCursorFrame()
{
    /* Blinking Cursor On Text Frame Setting */
    //~ contents_path /Mouse/Blinking cursor in text area
    mBlinkingCursorOnTextWidget = new SwitchWidget(tr("Blinking cursor in text area"), this, UkccFrame::Top);
    mBlinkingCursorOnTextWidget->setObjectName("Blinking cursor in text area");
}

void MouseUI::setCursorSpeedFrame()
{
    /* Cursor Speed Frame Setting */

    //~ contents_path /Mouse/Cursor speed
    mCursorSpeedWidget = new SliderWidget(tr("Cursor speed"), false, this, UkccFrame::Bottom);
    mCursorSpeedWidget->setObjectName("Cursor speed");
    mCursorSpeedWidget->setLeftText(tr("Slow"));
    mCursorSpeedWidget->setRightText(tr("Fast"));
    mCursorSpeedWidget->slider()->setSliderType(KSliderType::SmoothSlider);
    mCursorSpeedWidget->slider()->setMinimum(100);
    mCursorSpeedWidget->slider()->setMaximum(2500);
    mCursorSpeedWidget->slider()->setSingleStep(200);
    mCursorSpeedWidget->slider()->setPageStep(200);
    mCursorSpeedWidget->installEventFilter(this);
}

void MouseUI::initConnection()
{
    QByteArray mouseId(kMouseSchemas);
    QByteArray desktopId(kDesktopSchemas);
    QByteArray themeId(kThemeSchemas);
    if (QGSettings::isSchemaInstalled(mouseId) && QGSettings::isSchemaInstalled(desktopId)) {
        mMouseGsetting = new QGSettings(mouseId, QByteArray(), this);
        mDesktopGsetting = new QGSettings(desktopId, QByteArray(), this);
        mThemeSettings = new QGSettings(themeId, QByteArray(), this);

        initEnableStatus();

        connect(mDominantHandWidget, QOverload<int>::of(&RadioButtonWidget::buttonClicked),
                this, &MouseUI::dominantHandSlot);

        connect(mScrollDirectionWdiget, QOverload<int>::of(&RadioButtonWidget::buttonClicked),
                this, &MouseUI::scrollDirectionSlot);

        connect(mWheelSpeedWidget, &SliderWidget::valueChanged, this, &MouseUI::wheelSpeedSlot);

        connect(mDoubleClickIntervalWidget, &SliderWidget::valueChanged , this, &MouseUI::doubleClickIntervalSlot);

        connect(mPointerSpeedWidget, &SliderWidget::valueChanged, this, &MouseUI::pointerSpeedSlot);

        connect(mMouseAccelerationWidget, &SwitchWidget::stateChanged, this, &MouseUI::mouseAccelerationSlot);

        connect(mPointerPositionWidget, &SwitchWidget::stateChanged, this, &MouseUI::pointerPositionSlot);

        connect(mPointerSizeWidet, QOverload<int>::of(&RadioButtonWidget::buttonClicked),
                this, &MouseUI::pointerSizeSlot);

        connect(mBlinkingCursorOnTextWidget, &SwitchWidget::stateChanged, this, &MouseUI::blinkCursorOnTextSlot);

        connect(mCursorSpeedWidget, &SliderWidget::valueChanged, this, &MouseUI::cursorSpeedSlot);

        gsettingConnection();
    }
}

void MouseUI::gsettingConnection()
{
    // 命令行修改 gsetting 的 key 值图形界面做出相应改变
    connect(mMouseGsetting,&QGSettings::changed,[=] (const QString &key){
        if(key == "leftHanded") {
            bool handHabit = mMouseGsetting->get(kDominantHandKey).toBool();
            if (handHabit == true) {
                mDominantHandRightRadio->setChecked(true);
            } else {
                mDominantHandLeftRadio->setChecked(true);
            }
        } else if(key == "naturalScroll") {
            bool scrollDirection = mMouseGsetting->get(kScrollDirection).toBool();
            mScrollDirectionWdiget->buttonGroup()->blockSignals(true);
            if (scrollDirection) {
                mScrollDirectionReverseRadio->setChecked(true);
            } else {
                mScrollDirectionForwardRadio->setChecked(true);
            }
            mScrollDirectionWdiget->buttonGroup()->blockSignals(false);
        } else if(key == "wheelSpeed") {
            mWheelSpeedWidget->slider()->blockSignals(true);
            int value = mMouseGsetting->get(kWheelSpeedKey).toInt();
            if (value < 1 || value >10) {
                mMouseGsetting->reset(kWheelSpeedKey);
            }
            mWheelSpeedWidget->setValue(mMouseGsetting->get(kWheelSpeedKey).toInt());
            mWheelSpeedWidget->slider()->blockSignals(false);
        } else if(key == "doubleClick") {
            int dc = mMouseGsetting->get(kDoubleClickIntervalKey).toInt();
            mDoubleClickIntervalWidget->slider()->blockSignals(true);
            mDoubleClickIntervalWidget->setValue(dc);
            mDoubleClickIntervalWidget->slider()->blockSignals(false);
        } else if(key == "motionAcceleration") {
            mPointerSpeedWidget->slider()->blockSignals(true);
            mPointerSpeedWidget->setValue(static_cast<int>(mMouseGsetting->get(kPointerSpeedKey).toDouble()*100));
            mPointerSpeedWidget->slider()->blockSignals(false);
        } else if(key == "mouseAccel") {
            mMouseAccelerationWidget->blockSignals(true);
            mMouseAccelerationWidget->setChecked(mMouseGsetting->get(kMouseAccelerationKey).toBool());
            mMouseAccelerationWidget->blockSignals(false);
        } else if(key == "locatePointer") {
            mPointerPositionWidget->blockSignals(true);
            mPointerPositionWidget->setChecked(mMouseGsetting->get(kPointerPositionKey).toBool());
            mPointerPositionWidget->blockSignals(false);
        } else if(key == "cursorSize") {
            int PointerSize = mMouseGsetting->get(kPointerSizeKey).toInt();
            if (PointerSize == 24) {
                mPointerSizeSmallRadio->setChecked(true);
            } else if (PointerSize == 36) {
                mPointerSizeMediumRadio->setChecked(true);
            } else if (PointerSize == 48) {
                mPointerSizeLargeRadio->setChecked(true);
            } else {
                mMouseGsetting->reset(kPointerSizeKey);
            }
        }
    });

    connect(mDesktopGsetting,&QGSettings::changed,[=](const QString &key) {
        if (key == "cursorBlink") {
            mBlinkingCursorOnTextWidget->blockSignals(true);
            mBlinkingCursorOnTextWidget->setChecked(mDesktopGsetting->get(kBlinkCursorOnTextKey).toBool());
            mBlinkingCursorOnTextWidget->blockSignals(false);
            if (mDesktopGsetting->get(kBlinkCursorOnTextKey).toBool()) {
                mBlinkingCursorOnTextWidget->setRadiusType(UkccFrame::Top);
            } else {
                mBlinkingCursorOnTextWidget->setRadiusType(UkccFrame::Around);
            }
            mCursorSpeedWidget->setVisible(mDesktopGsetting->get(kBlinkCursorOnTextKey).toBool());
        }else if (key == "cursorBlinkTime") {
            int mValue = mCursorSpeedWidget->slider()->maximum() - mDesktopGsetting->get(kCursorSpeedKey).toInt() + mCursorSpeedWidget->slider()->minimum();
            mCursorSpeedWidget->slider()->blockSignals(true);
            mCursorSpeedWidget->setValue(mValue);
            mCursorSpeedWidget->slider()->blockSignals(false);
        }
    });
}

void MouseUI::initEnableStatus()
{
    //初始化惯用手, 左手：右键为主键，右手：左键为主键
    mDominantHandWidget->buttonGroup()->blockSignals(true);
    bool currentDominantHand = mMouseGsetting->get(kDominantHandKey).toBool();
    if (currentDominantHand == true) {
        mDominantHandRightRadio->setChecked(true);
    } else {
        mDominantHandLeftRadio->setChecked(true);
    }
    mDominantHandWidget->buttonGroup()->blockSignals(false);

    // 初始化滚动方向 true:界面与鼠标滚轮滚动方向相反(反向),false:界面与滚轮滚动方向相同(正向)
    mScrollDirectionWdiget->buttonGroup()->blockSignals(true);
    if (mMouseGsetting->keys().contains("naturalScroll")) {
        bool currentScrollDirection = mMouseGsetting->get(kScrollDirection).toBool();
        if (currentScrollDirection) {
            mScrollDirectionReverseRadio->setChecked(true);
        } else {
            mScrollDirectionForwardRadio->setChecked(true);
        }
    }
    mScrollDirectionWdiget->buttonGroup()->blockSignals(false);

    //初始化滚轮速度
    mWheelSpeedWidget->slider()->blockSignals(true);
    int value = mMouseGsetting->get(kWheelSpeedKey).toInt();
    if (value < 1 || value >10) {
        mMouseGsetting->reset(kWheelSpeedKey);
    }
    mWheelSpeedWidget->setValue(mMouseGsetting->get(kWheelSpeedKey).toInt());
    mWheelSpeedWidget->slider()->blockSignals(false);

    //初始化双击间隔时长
    mDoubleClickIntervalWidget->slider()->blockSignals(true);
    mDoubleClickIntervalWidget->setValue(mMouseGsetting->get(kDoubleClickIntervalKey).toInt());
    mDoubleClickIntervalWidget->slider()->blockSignals(false);

    //初始化指针速度
    mPointerSpeedWidget->slider()->blockSignals(true);
    mPointerSpeedWidget->setValue(static_cast<int>(mMouseGsetting->get(kPointerSpeedKey).toDouble()*100));
    mPointerSpeedWidget->slider()->blockSignals(false);

    //初始化鼠标加速
    mMouseAccelerationWidget->blockSignals(true);
    mMouseAccelerationWidget->setChecked(mMouseGsetting->get(kMouseAccelerationKey).toBool());
    mMouseAccelerationWidget->blockSignals(false);

    //初始化按 ctrl 键时显示指针位置
    mPointerPositionWidget->blockSignals(true);
    mPointerPositionWidget->setChecked(mMouseGsetting->get(kPointerPositionKey).toBool());
    mPointerPositionWidget->blockSignals(false);

    //初始化指针大小
    mPointerSizeWidet->buttonGroup()->blockSignals(true);
    int currentPointerSize = mMouseGsetting->get(kPointerSizeKey).toInt();
    if (currentPointerSize != 24 && currentPointerSize != 36 && currentPointerSize != 48) {
        mMouseGsetting->reset(kPointerSizeKey);
        currentPointerSize = mMouseGsetting->get(kPointerSizeKey).toInt();
    }
    if (currentPointerSize == 24) {
        mPointerSizeSmallRadio->setChecked(true);
    } else if (currentPointerSize == 36) {
        mPointerSizeMediumRadio->setChecked(true);
    } else if (currentPointerSize == 48) {
        mPointerSizeLargeRadio->setChecked(true);
    }
    mPointerSizeWidet->buttonGroup()->blockSignals(false);

    //初始化文本区域光标闪烁
    mBlinkingCursorOnTextWidget->blockSignals(true);
    mBlinkingCursorOnTextWidget->setChecked(mDesktopGsetting->get(kBlinkCursorOnTextKey).toBool());
    mBlinkingCursorOnTextWidget->blockSignals(false);

    //初始化光标速度
    mCursorSpeedWidget->slider()->blockSignals(true);
    mCursorSpeedWidget->setValue(mCursorSpeedWidget->slider()->maximum() - mDesktopGsetting->get(kCursorSpeedKey).toInt()
                                 + mCursorSpeedWidget->slider()->minimum());
    mCursorSpeedWidget->slider()->blockSignals(false);
    if (mDesktopGsetting->get(kBlinkCursorOnTextKey).toBool()) {
        mBlinkingCursorOnTextWidget->setRadiusType(UkccFrame::Top);
    } else {
        mBlinkingCursorOnTextWidget->setRadiusType(UkccFrame::Around);
    }
    mCursorSpeedWidget->setVisible(mDesktopGsetting->get(kBlinkCursorOnTextKey).toBool());
}


/* Slot */
void MouseUI::dominantHandSlot(int id)
{
    bool tmpLeftHand;

    // 左键主键：右手、left-handed=false，右键主键：左手、left-handed=true
    if (id == 0) {
        tmpLeftHand = false;
    } else {
        tmpLeftHand = true;
    }

    mMouseGsetting->set(kDominantHandKey, tmpLeftHand);

    Common::buriedSettings(name(), mDominantHandWidget->objectName(), QString("setting"));
}

void MouseUI::scrollDirectionSlot(int id)
{
    bool tmpScrollDirection;

    // true:界面与鼠标滚轮滚动方向相反(反向),false:界面与滚轮滚动方向相同(正向)
    if (id == 0) {
        tmpScrollDirection = false;
    } else {
        tmpScrollDirection = true;
    }

    if (mMouseGsetting->keys().contains("naturalScroll")) {
        mMouseGsetting->set(kScrollDirection, tmpScrollDirection);
    } else {
        qDebug() << "naturalScroll is not exist in org.ukui.peripherals-mouse";
    }

    Common::buriedSettings(name(), mScrollDirectionWdiget->objectName(), QString("setting"));
}

void MouseUI::wheelSpeedSlot()
{
    mMouseGsetting->set(kWheelSpeedKey, mWheelSpeedWidget->value());
}

void MouseUI::doubleClickIntervalSlot()
{
    mMouseGsetting->set(kDoubleClickIntervalKey, mDoubleClickIntervalWidget->value());
    qApp->setDoubleClickInterval(mDoubleClickIntervalWidget->value());
}

void MouseUI::pointerSpeedSlot(int value)
{
    mMouseGsetting->set(kPointerSpeedKey, static_cast<double>(value)/mPointerSpeedWidget->slider()->maximum() * 10);
}

void MouseUI::mouseAccelerationSlot(bool checked)
{
    mMouseGsetting->set(kMouseAccelerationKey, checked);

    Common::buriedSettings(name(), mMouseAccelerationWidget->objectName(), QString("setting"), checked ? "true":"false");
}

void MouseUI::pointerPositionSlot(bool checked)
{
    mMouseGsetting->set(kPointerPositionKey, checked);

    Common::buriedSettings(name(), mPointerPositionWidget->objectName(), QString("setting"), checked ? "true":"false");
}

void MouseUI::pointerSizeSlot(int id)
{
    int tmpPointerSize = id;

    mMouseGsetting->set(kPointerSizeKey, tmpPointerSize);

    Common::buriedSettings(name(), mPointerSizeWidet->objectName(), QString("setting"));
}

void MouseUI::blinkCursorOnTextSlot(bool checked)
{
    mCursorSpeedWidget->setVisible(checked);
    mDesktopGsetting->set(kBlinkCursorOnTextKey, checked);
    mThemeSettings->set(kBlinkCursorOnTextKey, checked);

    if (!checked) {
        mBlinkingCursorOnTextWidget->setRadiusType(UkccFrame::Around);
        mThemeSettings->set(kCursorSpeedKey, 0);
    } else {
        mBlinkingCursorOnTextWidget->setRadiusType(UkccFrame::Top);
        int mValue = mCursorSpeedWidget->slider()->maximum() - mCursorSpeedWidget->slider()->value() + mCursorSpeedWidget->slider()->minimum();
        mThemeSettings->set(kCursorSpeedKey, mValue);
    }

    Common::buriedSettings(name(), mBlinkingCursorOnTextWidget->objectName(), QString("setting"), checked ? "true":"false");
}

void MouseUI::cursorSpeedSlot()
{
    mDesktopGsetting->set(kCursorSpeedKey, mCursorSpeedWidget->slider()->maximum() - mCursorSpeedWidget->value()
                          + mCursorSpeedWidget->slider()->minimum());
    mThemeSettings->set(kCursorSpeedKey, mCursorSpeedWidget->slider()->maximum() - mCursorSpeedWidget->value()
                        + mCursorSpeedWidget->slider()->minimum());
}

bool MouseUI::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::FocusOut) {
        if (watched == mWheelSpeedWidget->slider()) {
            int value = mWheelSpeedWidget->value();
            Common::buriedSettings(name(), mWheelSpeedWidget->objectName(), QString("setting"), QString::number(value));
        } else if (watched == mDoubleClickIntervalWidget->slider()) {
            int value = mDoubleClickIntervalWidget->value();
            Common::buriedSettings(name(), mDoubleClickIntervalWidget->objectName(), QString("setting"), QString::number(value));

        } else if (watched == mPointerSpeedWidget->slider()) {
            int value = mPointerSpeedWidget->value();
            Common::buriedSettings(name(), mPointerSpeedWidget->objectName(), QString("setting"), QString::number(value));

        } else if (watched == mCursorSpeedWidget->slider()) {
            int value = mCursorSpeedWidget->value();
            Common::buriedSettings(name(), mCursorSpeedWidget->objectName(), QString("setting"), QString::number(value));

        }
    }
    return QObject::eventFilter(watched, event);
}
